
Ext.ns('App');

App.LoginDialog = function() {
    return {
        getForm: function() {
    	var locationStore = new Ext.data.Store({
        	autoLoad:false,
        	baseParams :{login:"login"},
			proxy: new Ext.data.HttpProxy({
				url : 'localtion/localtionList'
			}),
            remoteSort: false,
            reader: new Ext.data.JsonReader({
				totalProperty : 'totalCount',
				root : 'rows',
				idProperty : 'local_id',
				fields: [{
					name: 'local_id',
					type: 'string'
				},
				{
					name: 'local_name',
					type: 'string'
				}]
			})
        });
            var form = new Ext.form.FormPanel({
				url:'user/login',
                labelWidth: 80,
                buttonAlign: 'center',
                bodyStyle: 'padding:5px;',
                frame: true,
                defaultType: 'textfield',
                defaults: {
                    enableKeyEvents: true,
                    anchor: '90%',
                    allowBlank: false
                },
                items: [{
                    name: 'staff_code',
                    fieldLabel: '员工编码',
                    selectOnFocus :true,
                    listeners: {
                        scope: this,
                        keypress: function(field, e) {
                            if (e.getKey() == 13) {
                                var passwordField = form.getForm().findField('password');
                                if (passwordField) {
                                    passwordField.focus();
                                }
                            }
                        }
                    }
                }, {
                    name: 'password',
                    fieldLabel: '用户密码',
                    inputType: 'password',
                    listeners: {
                        scope: this,
                        keypress: function(field, e) {
                            if (e.getKey() == 13) {
                                var captchaField = form.getForm().findField('captcha');
                                if (captchaField) {
                                    captchaField.focus();
                                }
                            }
                        }
                    }
                },{
                	fieldLabel: '登陆地点',
            		xtype:'combo',
            		hiddenName :"login_localtion",
            		store: locationStore,
                    displayField:'local_name',
                    valueField :'local_id',
                    mode: 'remote',
                    triggerAction: 'all',
                    editable: false,
                    emptyText:'选择登陆地点....'
                }, {
                    name: 'captcha',
                    fieldLabel: '验证码',
                    listeners: {
                        scope: this,
                        keypress: function(field, e) {
                            if (e.getKey() == 13) {
                                this.submit();
                            }
                        }
                    }
                }, {
                    xtype: 'panel',
                    height: 100,
                    html: '<div style="margin:5px 0px 0px 84px"><a href="#"><img alt="如果看不清楚请单击图片更换图片。" onclick="this.src=\'commons/captcha.jsp?d=\'+new Date();" id="code" height="82" width="242" src="commons/captcha.jsp?d=' + new Date() + '" border="0"></a></div>',
                    border: false
                }, {
                    xtype: 'panel',
                    height: 30,
                    html: '<div style="margin:5px 0px 0px 84px;color:red">*如果图片不清晰请单击图片更换图片</div>',
                    border: false
                }],
                buttons: [{
                    text: '确定',
                    scope: this,
                    handler: function() {
                        this.submit();
                    }
                }, {
                    text: '重置',
                    scope: this,
                    handler: function() {
                        form.getForm().reset();
                    }
                },(
			        "<select name='styleswitcher_select' id='styleswitcher_select'>"+
			           "<option value='blue' selected='true'>蓝色主题</option>"+
			           "<option value='gray'>灰色主题</option>"+
//			           "<option value='silverCherry'>红色主题</option>"+
			        "</select>")
                ]
            });
            return form;
        },

        getDialog: function() {
            var form = this.getForm();
            var dlg = new Ext.Window({
                width: 400,
                height: 310,
                title: '物流管理系统',
                plain: true,
                closable: true,
                resizable: false,
                frame: true,
                layout: 'fit',
                closeAction: 'hide',
                border: false,
                modal: true,
                items: [form],
                listeners: {
            		beforehide: function () {
		            	return  confirm('关闭登陆框(关闭后如需再次打开请刷新页面)');
                    },
                    afterrender : function (){
                    	Ext.get('styleswitcher_select').on('change',function(e,select){
                            var name = select[select.selectedIndex].value;
                            setActiveStyleSheet(name);
                        });
                        var cookie = readCookie("style");
                        var title = cookie ? cookie : getPreferredStyleSheet();
                        Ext.get('styleswitcher_select').dom.value=title;
                    }
                }
            });
            this.form = form;
            return dlg;
        },

        submit: function() {
            if (this.form.getForm().isValid()) {
                this.form.getForm().submit({
					waitTitle:'登录系统',
					waitMsg:'正在登录系统……',
	    			scope:this,
					method:'post',
					params:"",
	    			success: function(form, action){
						var isSuccess = action.result.success;
	    				var info = action.result.info;
						if(isSuccess=='true') {
							//Ext.Msg.alert('提示', info);
//							window.open ('main.jsp', 'newwindow', "height="+window.screen.availHeight+",width="+window.screen.availWidth+",top=0, left=0,alwaysRaised=yes,toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no"); //这句要写成一行
//							window.opener = null;
//							window.open('', '_self');
//							window.close();
							window.location = 'main.jsp';
						}else {
							Ext.Msg.alert('提示', info);
						}
	    			},
	    			failure: function(form, action){
						var info = action.result.info;
	    				Ext.Msg.alert('提示', info);
					}
	    		});
            }
        },

        show: function() {
            if (!this.dlg) {
                this.dlg = this.getDialog();
            }
            this.dlg.show();
        }
    };
}();
